/*
 * Copyright Thoughtworks, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.thoughtworks.go.spark.spa.service;

import com.thoughtworks.go.config.CruiseConfig;
import com.thoughtworks.go.config.validation.GoConfigValidity;
import com.thoughtworks.go.domain.GoConfigRevision;
import com.thoughtworks.go.server.service.GoConfigService;
import com.thoughtworks.go.spark.spa.model.ConfigXmlViewModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AdminConfigXmlService {
    private final GoConfigService goConfigService;

    @Autowired
    public AdminConfigXmlService(GoConfigService goConfigService) {
        this.goConfigService = goConfigService;
    }

    public ConfigXmlViewModel getConfigXmlData() {
        try {
            CruiseConfig config = goConfigService.getCurrentConfig();
            GoConfigValidity configValidity = goConfigService.checkConfigFileValid();
            boolean configValid = configValidity.isValid();

            return new ConfigXmlViewModel(
                goConfigService.xml(),
                config.getMd5(),
                goConfigService.fileLocation(),
                configValid
            );
        } catch (Exception e) {
            throw new RuntimeException("Failed to load configuration data", e);
        }
    }

    public ConfigValidationResult validateConfig(String configContent) {
        try {
            // 使用现有的配置验证逻辑
            GoConfigValidity validity = goConfigService.checkConfigFileValid();
            if (validity.isValid()) {
                return new ConfigValidationResult(true, null);
            } else {
                // 如果是无效配置，需要转换为 InvalidGoConfig 来获取错误消息
                if (validity instanceof GoConfigValidity.InvalidGoConfig) {
                    GoConfigValidity.InvalidGoConfig invalidConfig = (GoConfigValidity.InvalidGoConfig) validity;
                    return new ConfigValidationResult(false, invalidConfig.errorMessage());
                } else {
                    return new ConfigValidationResult(false, "Configuration validation failed");
                }
            }
        } catch (Exception e) {
            return new ConfigValidationResult(false, e.getMessage());
        }
    }

    public static class ConfigValidationResult {
        private final boolean valid;
        private final String errorMessage;

        public ConfigValidationResult(boolean valid, String errorMessage) {
            this.valid = valid;
            this.errorMessage = errorMessage;
        }

        public boolean isValid() {
            return valid;
        }

        public String getErrorMessage() {
            return errorMessage;
        }
    }
}
